package com.extweb4j;

import org.snaker.jfinal.plugin.SnakerPlugin;

import cn.dreampie.mail.MailerPlugin;
import cn.dreampie.quartz.QuartzPlugin;

import com.extweb4j.core.interceptor.ExtInterceptor;
import com.extweb4j.web.plugin.ExtActiveRecordPlugin;
import com.extweb4j.web.route.ExtRoutes;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

public class WebConfig extends JFinalConfig {

	static {
		PropKit.use("web.properties");
	}

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		me.setViewType(ViewType.FREE_MARKER);
		me.setEncoding("UTF-8");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setBaseViewPath("/WEB-INF/views");
		//上传文件最大20M
		me.setMaxPostSize(1024 * 1024 * 21);
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add(new ExtRoutes());
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// mysql
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
				PropKit.get("user"), PropKit.get("password"));
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ExtActiveRecordPlugin arp = new ExtActiveRecordPlugin(druidPlugin);
		arp.setDialect(new MysqlDialect());
		arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
		me.add(arp);
		// 配置Snaker插件
		SnakerPlugin snakerPlugin = new SnakerPlugin(druidPlugin,PropKit.getProp().getProperties());
		me.add(snakerPlugin);
		// 配置EhCache插件
		me.add(new EhCachePlugin());
		// 配置定时任务插件
	    me.add(new QuartzPlugin("quartz.properties"));
	    // 配置发送邮件插件
	    me.add(new MailerPlugin("email.properties"));
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new ExtInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {

	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
	}
}
